<?php
class module_playlist extends module {
	
	function __call($name, $arg) {
		if (substr($name, 0, 4) == "play") {
			$doc = document::getInstance();
			$doc->addJS("templates/playlist/js/play.js");
			request::set("playlistPlayRequest", substr($name, 4));
			$this->output("play");
		} else {
			$this->output();
		}		
	}
	
	function video_list() {
		$limit = 10;
		$id = request::get("id");
		if (is_numeric($id)) {
			$playlist = new playlist($id);
		} else {
			$playlist = new playlist();
			$playlist->loadByField("name", $id);
		}
		$playlist->hit();
		$videos = $playlist->getContent(true, $limit);
		$this->attach("videos", $videos->getDataArray(true));		 
		$this->output();
	}
	
	function featuredList() {
		$jb = new playlist();
		$this->playlistNames = $jb->getListOfNames($featured = 1);
		$this->output("list");
	}
	
	/** ADMIN ACTIONS **/
	
	function admin() {
		config::getInstance()->style = "admin";
		$jb = new playlist();
		$this->playlistNames = $jb->getListOfNames($featured = 1); 
		$this->output();
	}
	
	function manage() {
		config::getInstance()->style = "admin";
		$id = request::get("id");
		$jb = new playlist($id);
		$this->playlistContents = $jb->getListOfVideos();		 
		$this->output();
	}
		
	function removeContent() {
		config::getInstance()->style = "admin";
		$id = request::get("id");
		$playlistContent = new playlistcontent($id);
		$playlist = $playlistContent->getPlaylist_id();
		$playlistContent->delete();
		document::getInstance()->setRedirect("playlist/manage?id=".$playlist);
	}
	
	function saveContent() {
		config::getInstance()->style = "admin";
		$playlist = request::post("playlist");
		$video = request::post("video");
		$playlistContent = new playlistcontent();
		$playlistContent->insert(array("playlist_id" => $playlist, "video_id" => $video));
		document::getInstance()->setRedirect("playlist/manage?id=".$playlist);
	}
	
}
?>